package com.android.settings.audio.peq.qr.decoder;

import static com.android.settings.audio.peq.qr.Constant.QR_SCHEMA_V1_PEQ;

import android.text.TextUtils;

import com.android.settings.audio.peq.qr.QrManager;
import com.android.settings.audio.peq.qr.annotation.QrImpl;
import com.android.settings.audio.peq.soundeffect.PluginDataManager;
import com.android.settings.audio.peq.util.Base64;
import com.android.settings.audio.peq.util.BuildConfig;
import com.android.settings.audio.peq.util.ByteUtil;
import com.android.settings.audio.peq.util.LogPlus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by We on 2023/5/24
 */
@QrImpl(id = QR_SCHEMA_V1_PEQ)
public class PeqV1Decoder extends QrManager.AbsDecoder {
    public PeqV1Decoder() {
        this.id = QR_SCHEMA_V1_PEQ; //by inject
    }

    @Override
    public boolean canDecode(String content) {
        return content.startsWith(this.id);
    }

    @Override
    public Object onDecoded(String content) {
        //todo 解析二维码内容&动作
        if (TextUtils.isEmpty(content)) {
            throw new IllegalArgumentException("str is null");
        }
        content = content.substring(this.id.length());
        PluginDataManager.PeqData peqData = new PluginDataManager.PeqData();
        byte[] data = Base64.decode(content);

        int readIndex = 0;
        int nameBytesLen = data[readIndex++] & 0xFF;
        if (BuildConfig.DEBUG) LogPlus.d("###nameBytesLen:" + nameBytesLen);

        byte[] nameBytes = new byte[nameBytesLen];
        System.arraycopy(data, readIndex, nameBytes, 0, nameBytes.length);
        readIndex += nameBytes.length;

        peqData.name = new String(nameBytes);

        int deviceBytesLen = data[readIndex++] & 0xFF;
        byte[] deviceBytes = new byte[deviceBytesLen];
        System.arraycopy(data, readIndex, deviceBytes, 0, deviceBytes.length);
        readIndex += deviceBytes.length;

        peqData.device = new String(deviceBytes);

        if (BuildConfig.DEBUG) LogPlus.d("###deviceBytesLen:" + deviceBytesLen);

        int descriptionBytesLen = data[readIndex++] & 0xFF;
        byte[] descriptionBytes = new byte[descriptionBytesLen];
        System.arraycopy(data, readIndex, descriptionBytes, 0, descriptionBytes.length);
        readIndex += descriptionBytes.length;

        peqData.description = new String(descriptionBytes);
        if (BuildConfig.DEBUG) LogPlus.d("###descriptionBytesLen:" + descriptionBytesLen);

        byte[] preampBytes = new byte[4];
        System.arraycopy(data, readIndex, preampBytes, 0, preampBytes.length);
        readIndex += preampBytes.length;

        peqData.pre_amp = ByteUtil.convertToFloat(preampBytes);

        //final int bandItemBytesCount = 4 * 5;
        final int bandItemsCount = 10;
        List<PluginDataManager.BandItem> bandItems = new ArrayList<>();
        peqData.bands = bandItems;
        for (int i = 0; i < bandItemsCount; ++i) {
            PluginDataManager.BandItem bandItem = new PluginDataManager.BandItem();
            bandItems.add(bandItem);

            bandItem.filter = data[readIndex++];

            byte[] freq = new byte[4];
            System.arraycopy(data, readIndex, freq, 0, freq.length);
            readIndex += freq.length;
            bandItem.freq = ByteUtil.convertToFloat(freq);

            bandItem.on = data[readIndex++];

            byte[] gain = new byte[4];
            System.arraycopy(data, readIndex, gain, 0, gain.length);
            readIndex += gain.length;
            bandItem.gain = ByteUtil.convertToFloat(gain);

            byte[] q = new byte[4];
            System.arraycopy(data, readIndex, q, 0, q.length);
            readIndex += q.length;
            bandItem.q = ByteUtil.convertToFloat(q);
        }
        return peqData;
    }
}
